<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Zoom Pinch GoJS Sample</title>
<meta name="description" content="Customize the pinch-zooming action of GoJS." />
<!-- Copyright 1998-2019 by Northwoods Software Corporation. -->
<meta charset="UTF-8">
<script src="../release/go.js"></script>
<script src="../assets/js/goSamples.js"></script>  <!-- this is only for the GoJS Samples framework -->
<script id="code">
  function init() {
    if (window.goSamples) goSamples();  // init for these samples -- you don't need to call this

    var $ = go.GraphObject.make;  // for conciseness in defining templates

    myDiagram = $(go.Diagram, "myDiagramDiv",  // create a Diagram for the DIV HTML element
                  {
                    initialContentAlignment: go.Spot.Center,  // center the content
                    "undoManager.isEnabled": true  // enable undo & redo
                  });

    // define a simple Node template
    myDiagram.nodeTemplate =
      $(go.Node, "Auto",  // the Shape will go around the TextBlock
        $(go.Shape, "RoundedRectangle", { strokeWidth: 0},
          // Shape.fill is bound to Node.data.color
          new go.Binding("fill", "color")),
        $(go.TextBlock,
          { margin: 8 },  // some room around the text
          // TextBlock.text is bound to Node.data.key
          new go.Binding("text", "key"))
      );

    // Override normal pinch zoom to zoom the selected node instead of the Diagram.

    var originalScale = 1;
    var startDistance = 0; // how far apart the touches are at the start
    var isPinching = false;
    // Initiates pinch-zooming on multi-touch devices.
    myDiagram.toolManager.standardPinchZoomStart = function() {
      var diagram = this.diagram;
      if (diagram === null) return;
      var e = diagram.lastInput;

      var first = e.getMultiTouchViewPoint(0, new go.Point());
      var second = e.getMultiTouchViewPoint(1, new go.Point());

      // event.isMultiTouch
      // call doCancel to stop the rest of typical toolManager.domousedown/domousemove behavior
      this.doCancel();

      var node = diagram.selection.first();
      if (node === null) return;

      originalScale = node.scale;
      var x = second.x - first.x;
      var y = second.y - first.y;
      var dist = Math.sqrt((x * x) + (y * y));
      startDistance = dist;
      e.bubbles = false;
      isPinching = true;
      myDiagram.startTransaction('zoom node');
    };

    // Continues pinch-zooming (started by {@link #standardPinchZoomStart} on multi-touch devices.
    myDiagram.toolManager.standardPinchZoomMove = function() {
      var diagram = this.diagram;
      if (diagram === null) return;
      var e = diagram.lastInput;

      // event.isMultiTouch
      // call doCancel to stop the rest of typical toolManager.domousedown/domousemove behavior
      this.doCancel();

      var first = e.getMultiTouchViewPoint(0, new go.Point());
      var second = e.getMultiTouchViewPoint(1, new go.Point());

      var x = second.x - first.x;
      var y = second.y - first.y;
      var dist = Math.sqrt((x * x) + (y * y));
      var scale = dist / startDistance;
      var oldskip = myDiagram.skipsUndoManager;
      var node = diagram.selection.first();
      if (node !== null) node.scale = originalScale * scale;
      e.bubbles = false;
    };

    myDiagram.toolManager.doMouseUp = function() {
      if (isPinching) {
        myDiagram.commitTransaction('zoom node');
        isPinching = false;
      }
      go.ToolManager.prototype.doMouseUp.call(this);
    }

    myDiagram.model = new go.GraphLinksModel(
    [
      { key: "Alpha", color: "lightblue" },
      { key: "Beta", color: "orange" },
      { key: "Gamma", color: "lightgreen" },
      { key: "Delta", color: "pink" }
    ],
    [
      { from: "Alpha", to: "Beta" },
      { from: "Alpha", to: "Gamma" },
      { from: "Beta", to: "Beta" },
      { from: "Gamma", to: "Delta" },
      { from: "Delta", to: "Alpha" }
    ]);
  }
</script>
</head>
<body onload="init()">
<div id="sample">
  <!-- The DIV for the Diagram needs an explicit size or else we won't see anything.
       This also adds a border to help see the edges of the viewport. -->
  <div id="myDiagramDiv" style="border: solid 1px black; width:400px; height:400px"></div>
  <p>
    This sample gives an example of customizing <a>Tool.standardPinchZoomStart</a> and <a>Tool.standardPinchZoomMove</a>.
    Select a node and pinch zoom to see the action modify the scale of the Node instead of the scale of the Diagram.
  </p>

</div>
</body>
</html>